<template>
  <div class="categoryList">
    <h1>一级菜单列表</h1>
    <el-table :data="topItems">
      <el-table-column prop="id" label="ID" width="220"></el-table-column>
      <el-table-column prop="name" label="分类名称"></el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="small"
            @click="$router.push(`/categories/edit/${scope.row.id}`)"
          >编辑</el-button>
          <el-button type="danger" size="small" @click="topRemove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <h1>二级菜单列表</h1>
    <el-table :data="secItems">
      <el-table-column prop="id" label="ID" width="220"></el-table-column>
      <el-table-column prop="name" label="分类名称"></el-table-column>
      <el-table-column prop="toplist.name" label="父级名称"></el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="small"
            @click="$router.push({ name: 'categoriesedit', params: { id: scope.row.id,Superiorid:scope.row.Superiorid}})"
          >编辑</el-button>
          <el-button type="danger" size="small" @click="secRemove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
export default {
  data() {
    return {
      topItems: [],
      secItems: []
    };
  },

  methods: {
    async topRemove(row) {
      this.$confirm(`此操作将永久删除${row.name}分类, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true
      }).then(async () => {
        const res = await this.$http.post(`admin/topdelete/${row.id}`);
        this.$message({
          type: "success",
          message: "删除成功!"
        });
        this.fetch();
      });
    },
    async secRemove(row) {
      this.$confirm(`此操作将永久删除${row.name}分类, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true
      }).then(async () => {
        const res = await this.$http.post(`admin/secdelete/${row.id}`);
        this.$message({
          type: "success",
          message: "删除成功!"
        });
        this.fetch();
      });
    },
    async fetch() {
      const topRes = await this.$http.post("admin/toplist");
      const secRes = await this.$http.post("admin/seclist");
      this.topItems = topRes.data.data;
      this.secItems = secRes.data.data;
    }
  },
  created() {
    this.fetch();
  }
};
</script>

